package com.das.mechanic_base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3BottomHomeCarAndColorAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.CarPicAndColorBean;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class X3BottomHomeCarPicAndColorDialog extends X3BaseBottomSheetDialog implements X3BottomHomeCarAndColorAdapter.IOnClickCarPic {

    private RecyclerView rlv_pic;
    private ProgressBar pb_bar;
    private TextView tv_cancel;
    private X3BottomHomeCarAndColorAdapter carAndColorAdapter;
    private CarPicAndColorBean picBean;
    private List<CarPicAndColorBean> mList;
    private ValueAnimator animator;
    private long carId;

    private CarPicAndColorBean lastCarPicBean;

    public X3BottomHomeCarPicAndColorDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    public X3BottomHomeCarAndColorAdapter getBottomHomeCarAndColorAdapter() {
        return carAndColorAdapter;
    }

    @Override
    protected void initView() {
        rlv_pic = getView(R.id.rlv_pic);
        pb_bar = getView(R.id.pb_bar);
        tv_cancel = getView(R.id.tv_cancel);

        tv_cancel.setOnClickListener(view -> dismiss());

        rlv_pic.setLayoutManager(new GridLayoutManager(mContext,2));
        carAndColorAdapter = new X3BottomHomeCarAndColorAdapter(mContext,rlv_pic);
        rlv_pic.setAdapter(carAndColorAdapter);
        carAndColorAdapter.setiOnClickCarPic(this);

        /**
         * 解决点击事件需要点击两次才能生效
         */
        rlv_pic.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int currState = -1;
            Handler mHandler = new Handler();
            Runnable mRunnable = new Runnable() {
                @Override
                public void run() {
                    currState = -1;
                    rlv_pic.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, 0f, 0f, 0));
                }
            };

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                currState = newState;
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果在持续滚动中，则取消回调
                mHandler.removeCallbacks(mRunnable);
                //当前RecyclerView在滚动设置到某个位置的动画状态，代码调用时或者惯性滚动时就是这个状态
                if (currState == RecyclerView.SCROLL_STATE_SETTLING) {
                    mHandler.postDelayed(mRunnable, 20);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_car_pic_and_color_dialog;
    }

    IOnNextCarPic nextCarPic;

    public void setNextCarPic(IOnNextCarPic nextCarPic) {
        this.nextCarPic = nextCarPic;
    }

    public void changeNoToast() {
        if (carAndColorAdapter != null) {
            carAndColorAdapter.changeNoToast();
        }
    }

    public void sendCarId(long carId) {
        this.carId = carId;
        if (carAndColorAdapter != null) {
            carAndColorAdapter.sendCarId(carId);
        }
    }

    public interface IOnNextCarPic {
        void iOnNextGetCarPic(CarPicAndColorBean carPicAndColorBean,String carMove,int position);

        /**
         * 上传切割的时候需要默认选中
         * @param carPicAndColorBean
         */
        void iOnNextSelectPic(CarPicAndColorBean carPicAndColorBean);
    }

    public void changeCarAndPic(List<CarPicAndColorBean> carList,String carNum,long colorId,long carPicTempletId) {
        this.mList = new ArrayList<>();
        this.mList = carList;

        pb_bar.setVisibility(View.GONE);
        if (X3StringUtils.isListEmpty(mList)) {
            mList = new ArrayList<>();
        }

        if (X3StringUtils.isListEmpty(mList)) {
            CarPicAndColorBean picAndColorBean = new CarPicAndColorBean();
            picAndColorBean.setColorId(-1);
            picAndColorBean.setCarPicTempletId(-1);
            mList.add(picAndColorBean);
        }else {
            CarPicAndColorBean carPicAndColorBean = new CarPicAndColorBean();
            int index = -1;
            for (CarPicAndColorBean colorBean : mList) {
                if (!X3StringUtils.isEmpty(colorBean.getCarNum())) {
                    carPicAndColorBean = colorBean;
                    index = mList.indexOf(colorBean);
                    break;
                }
            }

            if (index != -1) {
                mList.remove(index);
            }else {
                carPicAndColorBean.setColorId(-1);
                carPicAndColorBean.setCarPicTempletId(-1);
            }
            mList.add(carPicAndColorBean);
        }
        lastCarPicBean = mList.get(mList.size() - 1);
        if (X3StringUtils.isEmpty(lastCarPicBean.getCarNum())) {
            lastCarPicBean = new CarPicAndColorBean();
        }

        if (carAndColorAdapter != null && !rlv_pic.isComputingLayout()) {
            carAndColorAdapter.changeCarAndColor(mList, carNum,colorId,carPicTempletId);
            carAndColorAdapter.sendCarId(carId);
        }
    }

    public void changeSelectColor(long colorId,long carPicTempletId) {
        if (carAndColorAdapter != null && !rlv_pic.isComputingLayout()) {
            carAndColorAdapter.changeSelectColor(colorId,carPicTempletId);
        }
    }

    /**
     * 车辆切割图上传成功刷新
     * @param colorBean
     */
    public void changeLastCarAndPic(CarPicAndColorBean colorBean) {
        if (X3StringUtils.isListEmpty(mList)) {
            return;
        }
        if (colorBean == null) {
            colorBean = lastCarPicBean;
        }else {
            lastCarPicBean = colorBean;
            this.picBean = colorBean;
            if (nextCarPic != null) {
                nextCarPic.iOnNextGetCarPic(picBean,"",0);
            }
        }
        mList.remove(mList.size() - 1);
        mList.add(colorBean);

        if (carAndColorAdapter != null) {
            carAndColorAdapter.changeLastCarCut(mList);
        }
    }

    public void showPrograssBar() {
        pb_bar.setVisibility(View.VISIBLE);
    }

    public void hidePrograssBar() {
        pb_bar.setVisibility(View.GONE);
    }

    @Override
    public void show() {
        super.show();

        if (!X3StringUtils.isListEmpty(mList)) {
            hidePrograssBar();
        }
    }

    /**
     * 传入要上传的车辆切割图
     * @param imgPath
     */
    public void changeCarCutImage(String imgPath) {
        if (X3StringUtils.isListEmpty(mList)) {
            mList = new ArrayList<>();
            mList.add(new CarPicAndColorBean());
        }

        CarPicAndColorBean carPicAndColorBean = mList.get(mList.size() - 1);
        carPicAndColorBean.setResourceUrl(imgPath);
        mList.remove(mList.size() - 1);
        mList.add(carPicAndColorBean);

        long colorId = carPicAndColorBean.getColorId();
        long carPicTempletId = carPicAndColorBean.getCarPicTempletId();

        initAnim();
        if (carAndColorAdapter != null) {
            carAndColorAdapter.changeLastCarCut(mList);
            carAndColorAdapter.sendCarId(carId);
            carAndColorAdapter.changeSelectColor(colorId,carPicTempletId);
        }

        if (nextCarPic != null) {
            CarPicAndColorBean picAndColorBean = mList.get(mList.size() - 1);
            picAndColorBean.setWaitingForCarPicTemplet(true);
            nextCarPic.iOnNextSelectPic(picAndColorBean);
        }

    }

    private void initAnim() {
        animator = ValueAnimator.ofFloat(0,0.9f);
        animator.setDuration(1000 * 20);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addUpdateListener(animation -> {
            float animatedValue = (float) animation.getAnimatedValue();
            if (isShowing() && carAndColorAdapter != null && !rlv_pic.isComputingLayout()) {
                carAndColorAdapter.changePrograss(animatedValue);
            }

        });
        animator.start();
    }

    @Override
    public void iOnClickCarPicId(CarPicAndColorBean picAndColorBean,String carMove,int position) {
        this.picBean = picAndColorBean;
        dismiss();
        if (nextCarPic != null) {
            nextCarPic.iOnNextGetCarPic(picBean,carMove,position);
        }
    }

    @Override
    public void iOnCancelAnim() {
        if (animator != null) {
            animator.cancel();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("接车单车款缩略图弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("接车单车款缩略图弹窗");
    }
}
